<?php
namespace app\api\controller;
use think\Db;
use think\Controller;
class Find extends Controller
{
  public function AjaxReturn($code,$msg,$data=array())
	{

		$returnData=array(
			"code"=>$code
		);
		if($msg)
		{
		  $returnData["message"]=$msg;
		}
		if(!empty($data))
		{
			$returnData["data"]=$data;
		}
		exit(json_encode($returnData));
	}
   public function chakan($openid){
   		$id=db('user')->where('code',$openid)->value('uid');
        return $id;
   }
	//发现页面显示
    public function index()
    {
		$openid=$this->chakan(input('openid'));
		if(input('name')!=""){
			$name=input('name');
			$where ['mflg'] = 1;
			$where ['f_message.stoptime'] = array("egt",date("Y-m-d H:i:s"));
			$rs=db("message")
			->alias('me')
			->join('f_user us','me.uid=us.uid','left')
			->join('f_real re','us.uid=re.uid','left')
			->where($where)
			->where('title','like','%'.$name.'%')
		    ->field('me.mid,me.type,address,me.price,me.number,me.unit,me.address,nickname,uname,us.uname,me.createtime,me.picture,me.title,re.rflg,me.faxian,me.content,us.gflg')
          ->order("me.state=1 desc,me.faxian=1 desc,me.createtime DESC")
			->select();
          $url="http://www.mamingwang.cn/upload/message/";
			 foreach ($rs as $key=>$value)
            {
                if($value['gflg']=="1")
                {
                    $rs[$key]['nickname']='农厂汇官网';
                }

               elseif($value['uname']=='')
               {
                 $rs[$key]['nickname']=$rs[$key]['nickname'];
               }
               elseif($value['rflg']=="1"){
               	  $rs[$key]['nickname']=$rs[$key]['uname'];	
               }
               if($value['unit']=="1")
                {
                    $rs[$key]['unit']='吨';
                }
               if($value['unit']=="2")
                {
                    $rs[$key]['unit']='公斤';
                }
               if($value['unit']=="3")
                {
                    $rs[$key]['unit']='斤';
                }
               if($value['unit']=="4")
                {
                    $rs[$key]['unit']='个';
                }
               $a=$rs[$key]['picture'];
               $arr=explode(',',$a);
               if($arr[0]==""){
               		$arr[0]="no.jpg";
               }
               $rs[$key]['picture']=$url.$arr[0];

            }
       
			if($rs){
				return  $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return  $this->AjaxReturn("-1","暂无信息");
			}	
		}
		else{
			$page=input("page");
			//发现查询
			$where ['mflg'] = 1;
			$where['faxian']=1;
			$where ['f_message.stoptime'] = array("egt",date("Y-m-d H:i:s"));
			$rs2=db("message")
			->alias('me')
			->join('f_user','me.uid=f_user.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
			->where($where)
		    ->field('type,picture,f_user.uid,address,me.mid,title,number,unit,nickname,uname,price,me.state,me.faxian,me.paixu,rflg,gflg,region,content,f_message.createtime')
			//->page($page,"7")
            ->order('me.state=1 DESC,me.paixu DESC')
			->select();
			//信息流中出去发现的信息
            $where ['mflg'] = 1;
            $where['faxian']=0;
            $where ['f_message.stoptime'] = array("egt",date("Y-m-d H:i:s"));
            $rs1=db("message")
                ->alias('me')
                ->join('f_user','me.uid=f_user.uid','left')
                ->join('f_real','f_user.uid=f_real.uid','left')
                ->where($where)
                ->field('type,picture,f_user.uid,address,me.mid,title,number,unit,nickname,uname,price,me.state,me.faxian,me.paixu,rflg,gflg,region,content,f_message.createtime')
                //->page($page,"7")
                ->order('me.createtime DESC')
                ->select();
            $rs=array_merge($rs2, $rs1);
//dump($rs);die;
           $url="http://www.mamingwang.cn/upload/message/";
			 foreach ($rs as $key=>$value)
            {
                if($value['gflg']=="1")
                {
                    $rs[$key]['nickname']='农厂汇官网';
                }
               elseif($value['uname']=='')
               {
                 $rs[$key]['nickname']=$rs[$key]['nickname'];
               }
               elseif($value['rflg']=="1"){
               	  $rs[$key]['nickname']=$rs[$key]['uname'];
               }
                if($value['unit']=="1")
                {
                    $rs[$key]['unit']='吨';
                }
               if($value['unit']=="2")
                {
                    $rs[$key]['unit']='公斤';
                }
               if($value['unit']=="3")
                {
                    $rs[$key]['unit']='斤';
                }
               if($value['unit']=="4")
                {
                    $rs[$key]['unit']='个';
                }
                $a=$rs[$key]['picture'];
                $arr=explode(',',$a);
               if($arr[0]==""){
               		$arr[0]="no.jpg";
               }
                $rs[$key]['picture']=$url.$arr[0];

            }
            $rs=arrayPage($rs,71,$page);
			if($rs){
				return  $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return  $this->AjaxReturn("-1","暂无信息");
			}		
		}		
    }
	//找产品搜索
	public function chanSearch(){
        $openid=$this->chakan(input('openid'));
		if(input('title')!=""){
		  $where ['mflg'] = 1;
          $where['type']=1;
			$where ['f_message.stoptime'] = array("egt",date("Y-m-d H:i:s"));
			$rs=db("message")
			->alias('me')
			->join('f_user us','me.uid=us.uid','left')
			->join('f_real re','us.uid=re.uid','left')
			->where($where)
			->where('title','like','%'.input('title').'%')
		     ->field('me.mid,me.type,me.price,us.uid,me.number,me.unit,me.address,us.uname,me.createtime,nickname,me.picture,me.title,re.rflg,me.faxian,me.content,us.gflg')
			->order("me.state=1 desc,me.faxian=1 desc,me.createtime DESC")
			->select();
           $url="http://www.mamingwang.cn/upload/message/";
			 foreach ($rs as $key=>$value)
            {
               if($value['gflg']=="1")
                {
                    $rs[$key]['nickname']='农厂汇官网';
                }
                elseif($value['uname']=='')
               {
                 $rs[$key]['nickname']=$rs[$key]['nickname'];
               }
               else
               {
                 $rs[$key]['nickname']=$rs[$key]['uname'];
               }
                if($value['unit']=="1")
                {
                    $rs[$key]['unit']='吨';
                }
               if($value['unit']=="2")
                {
                    $rs[$key]['unit']='公斤';
                }
               if($value['unit']=="3")
                {
                    $rs[$key]['unit']='斤';
                }
               if($value['unit']=="4")
                {
                    $rs[$key]['unit']='个';
                }
                $a=$rs[$key]['picture'];
                $arr=explode(',',$a);
               if($arr[0]==""){
               		$arr[0]="no.jpg";
               }
                $rs[$key]['picture']=$url.$arr[0];

            }
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
			
		}
		else{
			$page=input("page");
			$where ['mflg'] = 1;
             $where['type']=1;
			$where ['f_message.stoptime'] = array("egt",date("Y-m-d H:i:s"));
			$rs=db("message")
			->alias('me')
			->join('f_user us','me.uid=us.uid','left')
			->join('f_real re','us.uid=re.uid','left')
			->where($where)
            ->field('me.mid,me.type,me.price,me.number,us.uid,me.unit,me.address,us.uname,nickname,me.createtime,me.picture,me.title,re.rflg,me.faxian,me.content,us.gflg')
			->page($page,"10")
			->order("me.state=1 desc,me.faxian=1 desc,me.createtime DESC")
			->select();
           $url="http://www.mamingwang.cn/upload/message/";
			 foreach ($rs as $key=>$value)
            {
                if($value['gflg']=="1")
                {
                    $rs[$key]['nickname']='农厂汇官方';
                }
                elseif($value['uname']=='')
               {
                 $rs[$key]['nickname']=$rs[$key]['nickname'];
               }
               else
               {
                 $rs[$key]['nickname']=$rs[$key]['uname'];
               }
                 if($value['unit']=="1")
                {
                    $rs[$key]['unit']='吨';
                }
               if($value['unit']=="2")
                {
                    $rs[$key]['unit']='公斤';
                }
               if($value['unit']=="3")
                {
                    $rs[$key]['unit']='斤';
                }
               if($value['unit']=="4")
                {
                    $rs[$key]['unit']='个';
                }
                $a=$rs[$key]['picture'];
                $resque=explode(',',$a);
               if($resque[0]==""){
               	    $resque[0]="no.jpg";
               }
                $rs[$key]['picture']=$url.$resque[0];
            }
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}
		}    
	}
	//找公司搜索
	public function gongSearch(){
      $openid=$this->chakan(input('openid'));
		if(input('name')!=""){
           $where ['uflg'] = 1;
			$name=input('name');
			$rs=db("user")
			->join('f_company','f_user.uid=f_company.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_real.uid')
            ->where($where)
            ->where('f_user.uid','neq',$openid)
			->where('company','like','%'.$name.'%')
			->whereor('product','like','%'.$name.'%')
			->select();
           foreach($rs as  $key1=>$val1){ 
                $fuid=array_filter(explode(',',$val1['fuid']));
                  if(in_array($openid,$fuid))
                  {
                  		$rs[$key1]['fuid']='1';
                  }
                  else
                  {
                        $rs[$key1]['fuid']='0';
                  }
                  if($rs[$key1]['uname']=='')
                  {
                   $rs[$key1]['nickname']=$rs[$key1]['nickname'];
                  }
                 else
                 {
                   $rs[$key1]['nickname']=$rs[$key1]['uname'];
                 }
               }  
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
			
		}
		else{
			$page=input('page');
			$where ['uflg'] = 1;
			$rs=db("user")
			->join('f_company','f_user.uid=f_company.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_real.uid')
			->where($where)
            ->where('f_user.uid','neq',$openid)
			->page($page,"7")
			->select();
               foreach($rs as  $key1=>$val1){ 
                $fuid=array_filter(explode(',',$val1['fuid']));
                  if(in_array($openid,$fuid))
                  {
                  		$rs[$key1]['fuid']='1';
                  }
                  else
                  {
                        $rs[$key1]['fuid']='0';
                  }
                   if($rs[$key1]['uname']=='')
                  {
                   $rs[$key1]['nickname']=$rs[$key1]['nickname'];
                  }
                 else
                 {
                   $rs[$key1]['nickname']=$rs[$key1]['uname'];
                 }
               }   
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
		}     
	}
	//找老板搜索
	public function bosSearch(){
      $openid=$this->chakan(input('openid'));
		if(input('name')!=""){
            $where ['uflg'] = 1;
			$name=input('name');
			$rs=db("user")
			->join('f_company','f_user.uid=f_company.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_real.uid')
            ->where($where)
             ->where('f_user.uid','neq',$openid)
			->where('uname','like','%'.$name.'%')
            ->whereor('nickname','like','%'.$name.'%')
			->whereor('product','like','%'.$name.'%')
            ->whereor('phone','like','%'.$name.'%')
			->select();
             foreach($rs as  $key1=>$val1){ 
                $fuid=array_filter(explode(',',$val1['fuid']));
                  if(in_array($openid,$fuid))
                  {
                  		$rs[$key1]['fuid']='1';
                  }
                  else
                  {
                        $rs[$key1]['fuid']='0';
                  }
                  if($rs[$key1]['uname']=='')
                  {
                   $rs[$key1]['nickname']=$rs[$key1]['nickname'];
                  }
                 else
                 {
                   $rs[$key1]['nickname']=$rs[$key1]['uname'];
                 }
               }  
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
			
		}
		else{
			$page=input('page');
            $where ['uflg'] = 1;
			$rs=db("user")
			->join('f_company','f_user.uid=f_company.uid','left')
			->join('f_real','f_user.uid=f_real.uid','left')
            ->join('f_fans','f_fans.uid=f_real.uid')
			->where($where)
            ->where('f_user.uid','neq',$openid)
			->page($page,"7")
			->select();
             foreach($rs as  $key1=>$val1){ 
                $fuid=array_filter(explode(',',$val1['fuid']));
                  if(in_array($openid,$fuid))
                  {
                  		$rs[$key1]['fuid']='1';
                  }
                  else
                  {
                        $rs[$key1]['fuid']='0';
                  }
                   if($rs[$key1]['uname']=='')
                  {
                   $rs[$key1]['nickname']=$rs[$key1]['nickname'];
                  }
                 else
                 {
                   $rs[$key1]['nickname']=$rs[$key1]['uname'];
                 }
               }  
			if($rs){
				return $this->AjaxReturn("1","操作成功",$rs);
			}else{
				return $this->AjaxReturn("-1","暂无信息");
			}		
		}   
	}
}